package lct.spritecompiler;

import lct.util.PathUtility;
import lct.util.XMLUtility;

import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XMLConverter {
	public XMLConverter() {		
	}
	
	public void storeManifest(Manifest manifest, String filePath) {
		try {
			String directoryPath = PathUtility.getDirectoryFull(filePath);
			
			Document document = XMLUtility.createDocument();
			
			Element manifestElement = document.createElement("spriteconverter:manifest");
			{
				Element figureEntriesElement = document.createElement("figureEntries");
				for (Manifest.FigureEntry figureEntry : manifest.figureEntryVector) {
					Element figureEntryElement = document.createElement("figureEntry");
					{
						String figureRelativePath = PathUtility.makeRelative(directoryPath, figureEntry.filePath);
						XMLUtility.writeStringElement(document, "filePath", figureRelativePath, figureEntryElement);
					}
					figureEntriesElement.appendChild(figureEntryElement);
				}
				manifestElement.appendChild(figureEntriesElement);
				
				Element animationEntriesElement = document.createElement("animationEntries");
				for (Manifest.AnimationEntry animationEntry : manifest.animationEntryVector) {
					Element animationEntryElement = document.createElement("animationEntry");
					{
						String animationRelativePath = PathUtility.makeRelative(directoryPath, animationEntry.filePath);
						XMLUtility.writeStringElement(document, "filePath", animationRelativePath, animationEntryElement);
					}
					animationEntriesElement.appendChild(animationEntryElement);
				}
				manifestElement.appendChild(animationEntriesElement);
				
				Element textureStripEntriesElement = document.createElement("textureStripEntries");
				for (Manifest.TextureStripEntry textureStripEntry : manifest.textureStripEntryVector) {
					Element textureStripEntryElement = document.createElement("textureStripEntry");
					{
						String textureStripRelativePath = PathUtility.makeRelative(directoryPath, textureStripEntry.filePath);
						XMLUtility.writeStringElement(document, "filePath", textureStripRelativePath, textureStripEntryElement);
					}
					textureStripEntriesElement.appendChild(textureStripEntryElement);
				}
				manifestElement.appendChild(textureStripEntriesElement);
			}
			document.appendChild(manifestElement);
			
			XMLUtility.storeDocument(document, filePath);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println(e.getStackTrace());
		}
	}
	
	public Manifest loadManifest(String filePath) {
		try {
			String directoryPath = PathUtility.getDirectoryFull(filePath);
			
			Document document = XMLUtility.loadDocument(filePath);
			
			Element manifestElement = (Element)document.getElementsByTagName("spriteconverter:manifest").item(0);
			Manifest manifest = new Manifest();
			{
				Element figureEntriesElement = (Element)manifestElement.getElementsByTagName("figureEntries").item(0);
				NodeList figureEntryNodeList = figureEntriesElement.getElementsByTagName("figureEntry");
				manifest.figureEntryVector = new Vector<Manifest.FigureEntry>();
				for (int figureEntryIndex = 0; figureEntryIndex < figureEntryNodeList.getLength(); ++figureEntryIndex) {
					Element figureEntryElement = (Element)figureEntryNodeList.item(figureEntryIndex);
					Manifest.FigureEntry figureEntry = new Manifest.FigureEntry();
					{
						String figureRelativeFilePath = XMLUtility.readStringElement(figureEntryElement, "filePath", 0);
						figureEntry.filePath = PathUtility.combineFull(directoryPath, figureRelativeFilePath);
					}
					manifest.figureEntryVector.add(figureEntry);
				}
				
				Element animationEntriesElement = (Element)manifestElement.getElementsByTagName("animationEntries").item(0);
				NodeList animationEntryNodeList = animationEntriesElement.getElementsByTagName("animationEntry");
				manifest.animationEntryVector = new Vector<Manifest.AnimationEntry>();
				for (int animationEntryIndex = 0; animationEntryIndex < animationEntryNodeList.getLength(); ++animationEntryIndex) {
					Element animationEntryElement = (Element)animationEntryNodeList.item(animationEntryIndex);
					Manifest.AnimationEntry animationEntry = new Manifest.AnimationEntry();
					{
						String animationRelativeFilePath = XMLUtility.readStringElement(animationEntryElement, "filePath", 0);
						animationEntry.filePath = PathUtility.combineFull(directoryPath, animationRelativeFilePath);
					}
					manifest.animationEntryVector.add(animationEntry);
				}
				
				Element textureStripEntriesElement = (Element)manifestElement.getElementsByTagName("textureStripEntries").item(0);
				NodeList textureStripEntryNodeList = textureStripEntriesElement.getElementsByTagName("textureStripEntry");
				manifest.textureStripEntryVector = new Vector<Manifest.TextureStripEntry>();
				for (int textureStripEntryIndex = 0; textureStripEntryIndex < textureStripEntryNodeList.getLength(); ++textureStripEntryIndex) {
					Element textureStripEntryElement = (Element)textureStripEntryNodeList.item(textureStripEntryIndex);
					Manifest.TextureStripEntry textureStripEntry = new Manifest.TextureStripEntry();
					{
						String textureStripRelativeFilePath = XMLUtility.readStringElement(textureStripEntryElement, "filePath", 0);
						textureStripEntry.filePath = PathUtility.combineFull(directoryPath, textureStripRelativeFilePath);
					}
					manifest.textureStripEntryVector.add(textureStripEntry);
				}
			}
			return manifest;
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			
			return null;
		}
	}
}
